package com.hurl.waguji.biz.download;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.testng.log4testng.Logger;

import com.hurl.library.spider.UrlPattern;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.Formatter;
import com.hurl.waguji.base.dataprocess.ProcessParams;
import com.hurl.waguji.persistence.DmGpBO;
import com.hurl.waguji.persistence.JcsjGdrsThsBO;
import com.hurl.waguji.persistence.JcsjGdrsThsBOId;

class ProfileGdrsThsProcessor extends BaseDownloadForUpdateProcessor {

	public ProfileGdrsThsProcessor(ProcessParams params,
			DownloadIdentifier identifier) {
		super(params, identifier);
	}

	private static final Logger log = Logger.getLogger(ProfileGdrsThsProcessor.class);
	

	private List<JcsjGdrsThsBO> html2BO(String gpdm, String html){
		if(StringUtils.isBlank(html)){
			return null;
		}
		List<String> trlist = StringUtils.substringsBetween(html, "<tr>", "</tr>");
		if(trlist==null || trlist.isEmpty()){
			return null;
		}
		
		List<Date> rqlist = new ArrayList<Date>();
		List<Integer> gdrslist = new ArrayList<Integer>();
		List<Double> rjltglist = new ArrayList<Double>();
		List<Double> jsqbhlist = new ArrayList<Double>();
		
		//日期
		String tr = trlist.get(0);
		List<String> tdlist = StringUtils.substringsBetween(tr, "<td nowrap style=\"text-align:center\">", "</td>");
		if(tdlist==null || tdlist.isEmpty()){
			return null;
		}
		for(int i=0;i<tdlist.size();i++){
			rqlist.add(Formatter.toDate(tdlist.get(i)));
		}
		//股东人数(户)
		tr = trlist.get(1);
		tdlist = StringUtils.substringsBetween(tr, "<td><span>", "</span></td>");
		if(tdlist==null || tdlist.isEmpty()){
			return null;
		}
		for(int i=0;i<tdlist.size();i++){
			gdrslist.add(Formatter.toInteger(tdlist.get(i)));
		}
		//人均流通股(股)
		tr = trlist.get(2);
		tdlist = StringUtils.substringsBetween(tr, "<td><span>", "</span></td>");
		if(tdlist==null || tdlist.isEmpty()){
			return null;
		}
		for(int i=0;i<tdlist.size();i++){
			rjltglist.add(Formatter.toDouble(tdlist.get(i)));
		}
		//较上期变化(%)
		tr = trlist.get(3);
		tdlist = StringUtils.substringsBetween(tr, "<td><span>", "</span></td>");
		if(tdlist==null || tdlist.isEmpty()){
			return null;
		}
		for(int i=0;i<tdlist.size();i++){
			jsqbhlist.add(Formatter.toDouble(tdlist.get(i)));
		}
		
		//校验4个list长度是否都一样
		int total = rqlist.size()+gdrslist.size()+rjltglist.size()+jsqbhlist.size();
		boolean match = total==rqlist.size()*4 && total==gdrslist.size()*4 && total==rjltglist.size()*4 && total==jsqbhlist.size()*4;
		if(!match){
			log.error("股东人数、人均流通股、较上期变化、日期数据不对称");
			return null;
		}
		List<JcsjGdrsThsBO> bolist = new ArrayList<JcsjGdrsThsBO>();
		JcsjGdrsThsBO bo = null;
		for(int i=0;i<rqlist.size();i++){
			bo = new JcsjGdrsThsBO();
			bo.setId(new JcsjGdrsThsBOId(gpdm,rqlist.get(i)));
			bo.setGdrs(gdrslist.get(i));
			bo.setJsqbh(jsqbhlist.get(i));
			bo.setRjltg(rjltglist.get(i));
			bolist.add(bo);
		}
		return bolist;
	}
	
	@Override
	public void convertHTML2BO(String url, String html, BOSaver collector) {

		if(StringUtils.isBlank(html)){
			return;
		}
		List<JcsjGdrsThsBO> list = html2BO(context.getEnvAsString("gpdm"),html);
		
		if(list!=null && list.size()>0){
			for(JcsjGdrsThsBO bo : list){
				collector.addBO(bo);
			}
		}
	}

	@Override
	public UrlPattern getUrlPattern() {
		return new StockIteratorUrlPattern(context) {
			
			@Override
			public String bo2url(DmGpBO bo) {
				return "http://basic.10jqka.com.cn/"+bo.getGpDmJ()+"/gdyj_iFrame.html";
			}
		};
	}

}
